package com.wenhuitec.android.tang.util;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.bean.OrderProductModel;
import com.dhkj.web.bean.PrintReportModel;
import com.dhkj.web.bean.ReportModel;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.entity.DeviceCarModel;
import com.wenhuitec.android.tang.sysui.model.CarFoodsModel;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Locale;

public class BytesUtil {

    /**
     * 字节流转16进制字符串
     */
    public static String getHexStringFromBytes(byte[] data) {
        if (data == null || data.length <= 0) {
            return null;
        }
        String hexString = "0123456789ABCDEF";
        int size = data.length * 2;
        StringBuilder sb = new StringBuilder(size);
        for (int i = 0; i < data.length; i++) {
            sb.append(hexString.charAt((data[i] & 0xF0) >> 4));
            sb.append(hexString.charAt((data[i] & 0x0F) >> 0));
        }
        return sb.toString();
    }

    /**
     * 单字符转字节
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 16进制字符串转字节数组
     */
    @SuppressLint("DefaultLocale")
    public static byte[] getBytesFromHexString(String hexString) {
        if (hexString == null || "".equals(hexString)) {
            return null;
        }
        hexString = hexString.replace(" ", "");
        hexString = hexString.toUpperCase();
        int size = hexString.length() / 2;
        char[] hexArray = hexString.toCharArray();
        byte[] rv = new byte[size];
        for (int i = 0; i < size; i++) {
            int pos = i * 2;
            rv[i] = (byte) (charToByte(hexArray[pos]) << 4 | charToByte(hexArray[pos + 1]));
        }
        return rv;
    }

    /**
     * 十进制字符串转字节数组
     */
    @SuppressLint("DefaultLocale")
    public static byte[] getBytesFromDecString(String decString) {
        if (decString == null || "".equals(decString)) {
            return null;
        }
        decString = decString.replace(" ", "");
        int size = decString.length() / 2;
        char[] decarray = decString.toCharArray();
        byte[] rv = new byte[size];
        for (int i = 0; i < size; i++) {
            int pos = i * 2;
            rv[i] = (byte) (charToByte(decarray[pos]) * 10 + charToByte(decarray[pos + 1]));
        }
        return rv;
    }

    /**
     * 字节数组组合操作1
     */
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }


    /**
     * 字节数组组合操作2
     */
    public static byte[] byteMerger(byte[][] byteList) {
        int length = 0;
//        for (int i = 0; i < byteList.length; i++) {
//            length += byteList[i].length;
//        }
        for (byte[] bytes : byteList) {
            length += bytes.length;
        }
        byte[] result = new byte[length];

        int index = 0;
        for (int i = 0; i < byteList.length; i++) {
            byte[] nowByte = byteList[i];
            for (int k = 0; k < byteList[i].length; k++) {
                result[index] = nowByte[k];
                index++;
            }
        }
//        for (int i = 0; i < index; i++) {
//            // CommonUtils.LogWuwei("", "result[" + i + "] is " + result[i]);
//        }
        return result;
    }

    /**
     * 生成表格字节流
     */
    public static byte[] initTable(int h, int w) {
        int hh = h * 32;
        int ww = w * 4;
        byte[] data = new byte[hh * ww + 5];
        // xL
        data[0] = (byte) ww;
        // xH
        data[1] = (byte) (ww >> 8);
        data[2] = (byte) hh;
        data[3] = (byte) (hh >> 8);
        int k = 4;
        int m = 31;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                data[k++] = (byte) 0xFF;
                data[k++] = (byte) 0xFF;
                data[k++] = (byte) 0xFF;
                data[k++] = (byte) 0xFF;
            }
            if (i == h - 1) m = 30;
            for (int t = 0; t < m; t++) {
                for (int j = 0; j < w - 1; j++) {
                    data[k++] = (byte) 0x80;
                    data[k++] = (byte) 0;
                    data[k++] = (byte) 0;
                    data[k++] = (byte) 0;
                }
                data[k++] = (byte) 0x80;
                data[k++] = (byte) 0;
                data[k++] = (byte) 0;
                data[k++] = (byte) 0x01;
            }
        }
        for (int j = 0; j < w; j++) {
            data[k++] = (byte) 0xFF;
            data[k++] = (byte) 0xFF;
            data[k++] = (byte) 0xFF;
            data[k++] = (byte) 0xFF;
        }
        data[k++] = 0x0A;
        return data;
    }

    /**
     * 生成二维码字节流
     */
//    public static byte[] getZxingQrCode(String data, int size) {
//        try {
//            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
//            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//            // 图像数据转换，使用了矩阵转换
//            BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, size, size, hints);
//            return getBytesFromBitMatrix(bitMatrix);
//        } catch (WriterException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public static byte[] getBytesFromBitMatrix(BitMatrix bits) {
//        if (bits == null) {
//            return null;
//        }
//        int h = bits.getHeight();
//        int w = (bits.getWidth() + 7) / 8;
//        byte[] rv = new byte[h * w + 4];
//        // xL
//        rv[0] = (byte) w;
//        // xH
//        rv[1] = (byte) (w >> 8);
//        rv[2] = (byte) h;
//        rv[3] = (byte) (h >> 8);
//
//        int k = 4;
//        for (int i = 0; i < h; i++) {
//            for (int j = 0; j < w; j++) {
//                for (int n = 0; n < 8; n++) {
//                    byte b = getBitMatrixColor(bits, j * 8 + n, i);
//                    rv[k] += rv[k] + b;
//                }
//                k++;
//            }
//        }
//        return rv;
//    }
//
//    private static byte getBitMatrixColor(BitMatrix bits, int x, int y) {
//        int width = bits.getWidth();
//        int height = bits.getHeight();
//        if (x >= width || y >= height || x < 0 || y < 0) return 0;
//        if (bits.get(x, y)) {
//            return 1;
//        } else {
//            return 0;
//        }
//    }

    /**
     * 将bitmap图转换为头四位有宽高的光栅位图
     */
    public static byte[] getBytesFromBitMap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int bw = (width - 1) / 8 + 1;

        byte[] rv = new byte[height * bw + 4];
        // xL
        rv[0] = (byte) bw;
        // xH
        rv[1] = (byte) (bw >> 8);
        rv[2] = (byte) height;
        rv[3] = (byte) (height >> 8);

        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int clr = pixels[width * i + j];
                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                byte gray = (RGB2Gray(red, green, blue));
                rv[(width * i + j) / 8 + 4] = (byte) (rv[(width * i + j) / 8 + 4] | (gray << (7 - ((width * i + j) % 8))));
            }
        }
        return rv;
    }

    /**
     * 将bitmap转成按mode指定的N点行数据
     */
    public static byte[] getBytesFromBitMap(Bitmap bitmap, int mode) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        if (mode == 0 || mode == 1) {
            byte[] res = new byte[width * height / 8 + 5 * height / 8];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for (int i = 0; i < height / 8; i++) {
                res[0 + i * (width + 5)] = 0x1b;
                res[1 + i * (width + 5)] = 0x2a;
                res[2 + i * (width + 5)] = (byte) mode;
                res[3 + i * (width + 5)] = (byte) (width % 256);
                res[4 + i * (width + 5)] = (byte) (width / 256);
                for (int j = 0; j < width; j++) {
                    byte gray = 0;
                    for (int m = 0; m < 8; m++) {
                        int clr = pixels[j + width * (i * 8 + m)];
                        int red = (clr & 0x00ff0000) >> 16;
                        int green = (clr & 0x0000ff00) >> 8;
                        int blue = clr & 0x000000ff;
                        gray = (byte) ((RGB2Gray(red, green, blue) << (7 - m)) | gray);
                    }
                    res[5 + j + i * (width + 5)] = gray;
                }
            }
            return res;
        } else if (mode == 32 || mode == 33) {
            byte[] res = new byte[width * height / 8 + 5 * height / 24];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            for (int i = 0; i < height / 24; i++) {
                res[0 + i * (width * 3 + 5)] = 0x1b;
                res[1 + i * (width * 3 + 5)] = 0x2a;
                res[2 + i * (width * 3 + 5)] = (byte) mode;
                res[3 + i * (width * 3 + 5)] = (byte) (width % 256);
                res[4 + i * (width * 3 + 5)] = (byte) (width / 256);
                for (int j = 0; j < width; j++) {
                    for (int n = 0; n < 3; n++) {
                        byte gray = 0;
                        for (int m = 0; m < 8; m++) {
                            int clr = pixels[j + width * (i * 24 + m + n * 8)];
                            int red = (clr & 0x00ff0000) >> 16;
                            int green = (clr & 0x0000ff00) >> 8;
                            int blue = clr & 0x000000ff;
                            gray = (byte) ((RGB2Gray(red, green, blue) << (7 - m)) | gray);
                        }
                        res[5 + j * 3 + i * (width * 3 + 5) + n] = gray;
                    }
                }
            }
            return res;
        } else {
            return new byte[]{0x0A};
        }

    }

    private static byte RGB2Gray(int r, int g, int b) {
        return (false ? ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) > 200)
                : ((int) (0.29900 * r + 0.58700 * g + 0.11400 * b) < 200)) ? (byte) 1 : (byte) 0;
    }

    /**
     * 生成间断性黑块数据
     *
     * @param w : 打印纸宽度, 单位点
     */
    public static byte[] initBlackBlock(int w) {
        int ww = (w + 7) / 8;
        int n = (ww + 11) / 12;
        int hh = n * 24;
        byte[] data = new byte[hh * ww + 5];
        // xL
        data[0] = (byte) ww;
        // xH
        data[1] = (byte) (ww >> 8);
        data[2] = (byte) hh;
        data[3] = (byte) (hh >> 8);

        int k = 4;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 24; j++) {
                for (int m = 0; m < ww; m++) {
                    if (m / 12 == i) {
                        data[k++] = (byte) 0xFF;
                    } else {
                        data[k++] = 0;
                    }
                }
            }
        }
        data[k++] = 0x0A;
        return data;
    }

    /**
     * 生成一大块黑块数据
     *
     * @param h : 黑块高度, 单位点
     * @param w : 黑块宽度, 单位点, 8的倍数
     */
    public static byte[] initBlackBlock(int h, int w) {
        int hh = h;
        int ww = (w - 1) / 8 + 1;
        byte[] data = new byte[hh * ww + 6];
        // xL
        data[0] = (byte) ww;
        // xH
        data[1] = (byte) (ww >> 8);
        data[2] = (byte) hh;
        data[3] = (byte) (hh >> 8);

        int k = 4;
        for (int i = 0; i < hh; i++) {
            for (int j = 0; j < ww; j++) {
                data[k++] = (byte) 0xFF;
            }
        }
        data[k++] = 0x00;
        data[k++] = 0x00;
        return data;
    }

    /**
     * 基本覆盖热敏小票打印中所有epson指令的操作
     */
    public static byte[] customData() {
        byte[] rv = new byte[]{
                (byte) 0xB4, (byte) 0xF2, (byte) 0xD3, (byte) 0xA1, (byte) 0xBB, (byte) 0xFA, (byte) 0xD7, (byte) 0xD4, (byte) 0xBC, (byte) 0xEC, 0x0A,
                // 打印机自检
                0x1F, 0X1B, 0x1F, 0x53,
                // 初始化打印机
                0x1B, 0x40,
                // 分割线---
                0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A,
                (byte) 0xB8, (byte) 0xC4, (byte) 0xB1, (byte) 0xE4, (byte) 0xD7, (byte) 0xD6, (byte) 0xBC, (byte) 0xE4, (byte) 0xBE, (byte) 0xE0, 0x0A,
                0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A,
                // 设置字符右间距
                0x30, 0x2E, 0x30, 0x3A, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, 0x0A,
                0x30, 0x2E, 0x35, 0x3A, 0x1B, 0x20, 0x0C, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, 0x0A,
                0x1B, 0x20, 0x00,
                0x31, 0x2E, 0x30, 0x3A, 0x1B, 0x20, 0x18, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, 0x0A,
                0x1B, 0x20, 0x00,
                0x32, 0x2E, 0x30, 0x3A, 0x1B, 0x20, 0x30, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, (byte) 0xB2, (byte) 0xE2, (byte) 0xCA, (byte) 0xD4, 0x0A,
                0x1B, 0x20, 0x00,
                // 分割线===
                0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x0A,
                (byte) 0xD7, (byte) 0xD6, (byte) 0xCC, (byte) 0xE5, (byte) 0xD0, (byte) 0xA7, (byte) 0xB9, (byte) 0xFB, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x0A,
                // 字体效果 商米科技
                // 设置加粗、倍高、倍宽、下划线、反白
                0x1B, 0x21, 0x08, (byte) 0xBC, (byte) 0xD3, (byte) 0xB4, (byte) 0xD6, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1B, 0x21, 0x00,
                0x1B, 0x45, 0x01, (byte) 0xBC, (byte) 0xD3, (byte) 0xB4, (byte) 0xD6, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1B, 0x45, 0x00,
                0x1B, 0x21, 0x10, (byte) 0xB1, (byte) 0xB6, (byte) 0xB8, (byte) 0xDF, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1B, 0x21, 0x20, (byte) 0xB1, (byte) 0xB6, (byte) 0xBF, (byte) 0xED, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1D, 0x21, 0x11, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC,
                0x1D, 0x21, 0x22, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1D, 0x21, 0x00,
                0x1B, 0x21, (byte) 0x80, (byte) 0xCF, (byte) 0xC2, (byte) 0xBB, (byte) 0xAE, (byte) 0xCF, (byte) 0xDF, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1B, 0x21, 0x00,
                0x1B, 0x2D, 0x01, (byte) 0xCF, (byte) 0xC2, (byte) 0xBB, (byte) 0xAE, (byte) 0xCF, (byte) 0xDF, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1B, 0x2D, 0x00,
                0x1D, 0x42, 0x01, 0x1B, 0x21, 0x08, (byte) 0xB7, (byte) 0xB4, (byte) 0xB0, (byte) 0xD7, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, (byte) 0xC9, (byte) 0xCC, (byte) 0xC3, (byte) 0xD7, (byte) 0xBF, (byte) 0xC6, (byte) 0xBC, (byte) 0xBC, 0x0A,
                0x1D, 0x42, 0x00, 0x1B, 0x21, 0x00,

        };
        return rv;
    }


    public static byte[] printReport(PrintReportModel printReportModel, String user) {
        try {

            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText(printReportModel.getAddrName() == null ? "" : printReportModel.getAddrName(), (byte) 0xff);
            pUtil.printLine();
            pUtil.printLargeText(printReportModel.getTimeName() == null ? "" : printReportModel.getTimeName(), (byte) 0xff);
            pUtil.printLine();
//            pUtil.printLine();
            pUtil.printLargeText("机号:" + user, (byte) 60);
//            for (int i = 0; i < 255; i++) {
//                pUtil.printLargeText(String.valueOf(i) + "机号:" + user, (byte) i);
//                pUtil.printLine();
//            }
            pUtil.printLine();
            pUtil.printLine();
            pUtil.printAlignment(0);
//            pUtil.printTwoColumn("操作员:", user);
//            pUtil.printLine();
            pUtil.printTwoColumn("打印日期:", printReportModel.getPrintTime());
            pUtil.printLine();
            pUtil.printTwoColumn("打印时间段:", printReportModel.getReportTime());
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", printReportModel.getReportDate());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printBo(0);
            pUtil.printThreeColumn("", "名称", "数量", " 小计 ");
            pUtil.printBo(1);
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            BigDecimal priceBig = new BigDecimal(0);
            BigDecimal countBig = new BigDecimal(0);
            for (ReportModel goo : printReportModel.getReportModelList()) {
                priceBig = priceBig.add(new BigDecimal(goo.getAmountPrice()));
                countBig = countBig.add(new BigDecimal(goo.getAmountCount()));
                pUtil.printThreeColumn("", printReportModel.getAddrName(), "x" + String.valueOf(goo.getAmountCount()), "￥" + String.valueOf(goo.getAmountPrice()));
            }
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计数量:", countBig.stripTrailingZeros().toPlainString());
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", priceBig.stripTrailingZeros().toPlainString());
            pUtil.printLine();


            //微信支付
            ReportModel reportModel = printReportModel.getReportModelList().stream().filter(u -> u.getPayChannel() == 1).findFirst().orElse(null);
            if (reportModel != null) {
                pUtil.printLine();
                pUtil.printAlignment(1);
                pUtil.printLargeText("微信支付", (byte) 0x48);
                pUtil.printLine();
                pUtil.printAlignment(0);
                pUtil.printLine();
                pUtil.printLine();
                //打印商品列表
                pUtil.printBo(0);
                pUtil.printThreeColumn("", "名称", "数量", " 小计 ");
                pUtil.printBo(1);
                pUtil.printLine();
                pUtil.printDashLine();
                pUtil.printLine();
                pUtil.printThreeColumn("", printReportModel.getAddrName(),
                        "x" + String.valueOf(reportModel.getAmountCount()),
                        "￥" + String.valueOf(reportModel.getAmountPrice()));
                pUtil.printLine();
                pUtil.printDashLine();
                pUtil.printLine();
                pUtil.printTwoColumn("合计数量:", reportModel.getAmountCount());
                pUtil.printLine();
                pUtil.printTwoColumn("合计金额:", reportModel.getAmountPrice());
                pUtil.printLine();
            }

            reportModel = printReportModel.getReportModelList().stream().filter(u -> u.getPayChannel() == 3).findFirst().orElse(null);
            if (reportModel != null) {
                pUtil.printLine();
                pUtil.printAlignment(1);
                pUtil.printLargeText("现金支付", (byte) 0x48);
                pUtil.printLine();
                pUtil.printAlignment(0);
                pUtil.printLine();
                pUtil.printLine();
                //打印商品列表
                pUtil.printBo(0);
                pUtil.printThreeColumn("", "名称", "数量", " 小计 ");
                pUtil.printBo(1);
                pUtil.printLine();
                pUtil.printDashLine();
                pUtil.printLine();
                pUtil.printThreeColumn("", printReportModel.getAddrName(),
                        "x" + String.valueOf(reportModel.getAmountCount()),
                        "￥" + String.valueOf(reportModel.getAmountPrice()));
                pUtil.printLine();
                pUtil.printDashLine();
                pUtil.printLine();
                pUtil.printTwoColumn("合计数量:", reportModel.getAmountCount());
                pUtil.printLine();
                pUtil.printTwoColumn("合计金额:", reportModel.getAmountPrice());
                pUtil.printLine();
            }
            pUtil.printLine(3);
            pUtil.printQZ();
            return pUtil.getOutbyte();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] printOrder(DeviceCarModel deviceCarModel) {
        try {

            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText(deviceCarModel.getAddrName() == null ? "" : deviceCarModel.getAddrName(), (byte) 0xff);
            pUtil.printLine();
            pUtil.printLine();
            pUtil.printLargeText(deviceCarModel.getLiushui() == null ? "" : deviceCarModel.getLiushui(), (byte) 0xff);
            pUtil.printLine();
            pUtil.printLine();
            pUtil.printLargeText("堂食", (byte) 0xff);
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date()));
            pUtil.printLine();
            pUtil.printTwoColumn("订单号:", deviceCarModel.getOrders());
            pUtil.printLine();
            pUtil.printTwoColumn("点餐员:", "");
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printBo(0);
            pUtil.printThreeColumn("", "菜名", "数量", " 小计 ");
            pUtil.printBo(1);
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            for (CarFoodsModel goo : deviceCarModel.getCarFoods()) {
                pUtil.printThreeColumn("", goo.getName(), "x" + String.valueOf(goo.getFoodNumber()), "￥" + String.valueOf(goo.getPrice()));
            }
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计数量:", deviceCarModel.getAmountCount());
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", deviceCarModel.getAmountPrice());
            pUtil.printLine();
            pUtil.printTwoColumn("支付方式:", deviceCarModel.getPayChannelName());
            pUtil.printLine();
            pUtil.printLine(3);
            pUtil.printQZ();
            return pUtil.getOutbyte();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 部分字符打印的分割线标志
     */
    public static byte[] wordData() {
        byte[] bytes = new byte[]{
                // 分割线===
                0x1B, 0x61, 0x00,
                0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x0A,
                (byte) 0xD7, (byte) 0xD6, (byte) 0xB7, (byte) 0xFB, (byte) 0xBC, (byte) 0xAF, (byte) 0xC9, (byte) 0xE8, (byte) 0xD6, (byte) 0xC3, 0x0A,
                0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D, 0x0A,
                0x1C, 0x26, 0x1C, 0x43, 0x00,
        };
        return bytes;
    }

    public static byte[] printTest() {
        try {
            PrintUtil pUtil = new PrintUtil();
            pUtil.printAlignment(1);
            pUtil.printLargeText("打印测试(START)", (byte) 0xff);
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("打印时间:", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.CHINA).format(new Date()));
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printText("这是一个测试!!");
            pUtil.printLine();
            pUtil.printText("这是一个测试!!");
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printBo(0);
            pUtil.printThreeColumn("", "左", "中", "右");
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printBo(1);
            pUtil.printLine();
            pUtil.printAlignment(1);
            pUtil.printLargeText("打印测试(END)", (byte) 0xff);
            pUtil.printLine();
            pUtil.printLine();
            pUtil.printLine(3);
            pUtil.printQZ();
            return pUtil.getOutbyte();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] printProduct(OrderProductModel orderProductModel) {
        try {
            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText("仓库系统", (byte) 0xff);
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", orderProductModel.getInDate());
            pUtil.printLine();
            pUtil.printTwoColumn("订单号:", orderProductModel.getDh());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printBo(0);
            pUtil.printThreeColumn("", "名称", "数量", " 小计 ");
            pUtil.printBo(1);
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            for (OrderProductModel.Product goo : orderProductModel.getProductList()) {
                pUtil.printThreeColumn("", goo.getDeviceName(), "x" + new BigDecimal(goo.getNumber()).stripTrailingZeros().toPlainString()
                        , "￥" + String.valueOf(goo.getPrice()));
            }
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计数量:", orderProductModel.getAmountCount());
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", orderProductModel.getAmountPrice());
            pUtil.printLine();
            pUtil.printLine();
            pUtil.printLine(3);
            pUtil.printQZ();
            return pUtil.getOutbyte();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static byte[] printProductPay(OrderProductModel orderProductModel, JSONObject jsonObject, JSONObject resJson) {
        try {
            int sc = resJson.getInteger("sc");
            BigDecimal kye = new BigDecimal(resJson.getJSONObject("fk").getString("kye"));
            BigDecimal btye = new BigDecimal(resJson.getJSONObject("fk").getString("btye"));
            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText("仓库系统", (byte) 0xff);
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date()));
            pUtil.printLine();
            pUtil.printTwoColumn("订单号:", resJson.getString("order") == null ? "" : resJson.getString("order"));
            pUtil.printTwoColumn("姓名:", resJson.getJSONObject("fk").getString("name"));
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printBo(0);
            pUtil.printThreeColumn("", "名称", "数量", " 小计 ");
            pUtil.printBo(1);
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            BigDecimal amountPrice = new BigDecimal(0);
            BigDecimal amountCount = new BigDecimal(0);
            JSONArray jsonArray = jsonObject.getJSONArray("cartModel").getJSONObject(0).getJSONArray("carFoods");
            for (int i = 0; i < jsonArray.size(); i++) {
                pUtil.printThreeColumn("", jsonArray.getJSONObject(i).getString("name"),
                        "x" + new BigDecimal(jsonArray.getJSONObject(i).getString("foodNumber"))
                                .stripTrailingZeros().toPlainString(),
                        "￥" + new BigDecimal(jsonArray.getJSONObject(i).getString("foodNumber"))
                                .multiply(new BigDecimal(jsonArray.getJSONObject(i).getString("price")))
                                .setScale(2).toPlainString());
                amountPrice = amountPrice.add(
                        new BigDecimal(jsonArray.getJSONObject(i).getString("foodNumber"))
                                .multiply(new BigDecimal(jsonArray.getJSONObject(i).getString("price")))

                );
                amountCount = amountCount.add(new BigDecimal(jsonArray.getJSONObject(i).getString("foodNumber")));
            }
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计数量:", amountCount.setScale(2).stripTrailingZeros().toPlainString());
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", amountPrice.setScale(2).stripTrailingZeros().toPlainString());
            pUtil.printLine();
            if (sc == 1) {
                pUtil.printTwoColumn("上次余额:", btye.add(amountPrice).setScale(2).stripTrailingZeros().toPlainString());
            } else {
                pUtil.printTwoColumn("上次余额:", kye.add(amountPrice).setScale(2).stripTrailingZeros().toPlainString());
            }

            pUtil.printLine();
            pUtil.printTwoColumn("本次自购余额:", kye.setScale(2).stripTrailingZeros().toPlainString());
            pUtil.printLine();
            pUtil.printTwoColumn("本次补贴余额:", btye.setScale(2).stripTrailingZeros().toPlainString());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine(3);
            pUtil.printQZ();
            return pUtil.getOutbyte();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
